package com.gaswell.canal.impl;

import com.gaswell.canal.AbastractCanalOperator;
import com.gaswell.mapper.AlarmRecordMapper;
import com.gaswell.mapper.DiagnosisNewMapper;
import com.gaswell.mapper.ParamsMapper;
import com.gaswell.pojo.AlarmRecord;
import com.gaswell.pojo.Diagnosis;
import com.gaswell.pojo.Params;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName NullCanalOperator
 * @Author xiaow
 * @DATE 2024/4/7 21:54
 * 数据失真，需要进行回溯处理
 **/
@Component(value = "jy1CanalOperator")
public class JY1CanalOperator extends AbastractCanalOperator {
    @Autowired
    private DiagnosisNewMapper diagnosisNewMapper;

    @Autowired
    private AlarmRecordMapper alarmRecordMapper;
    @Autowired
    private ParamsMapper paramsMapper;

    @Override
    public void operate(Diagnosis diagnosis) {
        /**
         * 检测是否连续N个点都是积液
         *
         */

        int isjy=0;
//      获取回溯数
        int N = 0;
        Params params = paramsMapper.selectByNameaAndJh("积液-报警1-N", diagnosis.getJh());
        if (params == null) {
            N = (int) paramsMapper.selectByNameaAndJh("积液-报警1-N", "all_well").getParam_value();
        } else {
            N = (int) params.getParam_value();
        }

        List<Diagnosis> dataByNTime = diagnosisNewMapper.findDataByNTime(N, diagnosis.getJh(), diagnosis.getCjsj());

        for (Diagnosis diagnosis1 : dataByNTime) {
            if(diagnosis1.getCategory().equals("jy1"))
                isjy++;
        }

        if(isjy==N){
            AlarmRecord alarmRecord = new AlarmRecord();
            alarmRecord.setTime(diagnosis.getCjsj());
            alarmRecord.setAlarm_category("积液(规则1)");
            alarmRecord.setRecord("连续" + String.valueOf(N) + "个点的标记值为积液");
            alarmRecord.setJh(diagnosis.getJh());
            alarmRecordMapper.insert(alarmRecord);
        }








        /**
         * 检测是否连续N个点中积液次数超够了K
         *
         */
        //           报警规则2对规则1标签进行判断
        Params params1 = paramsMapper.selectByNameaAndJh("积液-报警2-N", diagnosis.getJh());
        if (params == null) {
            N = (int) paramsMapper.selectByNameaAndJh("积液-报警2-N", "all_well").getParam_value();
        } else {
            N = (int) params1.getParam_value();
        }
        int K = 0;
        Params params3 = paramsMapper.selectByNameaAndJh("积液-报警2-K", diagnosis.getJh());
        if (params == null) {
            K = (int) paramsMapper.selectByNameaAndJh("积液-报警2-K", "all_well").getParam_value();
        } else {
            K = (int) params3.getParam_value();
        }


        dataByNTime = diagnosisNewMapper.findDataByNTime(N, diagnosis.getJh(), diagnosis.getCjsj());

        for (Diagnosis diagnosis1 : dataByNTime) {
            if(diagnosis1.getCategory().equals("jy1"))
                isjy++;
        }
        if (isjy >= K) {
            AlarmRecord alarmRecord = new AlarmRecord();

            alarmRecord.setTime(diagnosis.getCjsj());
            alarmRecord.setAlarm_category("积液(规则1)");
            alarmRecord.setRecord("连续" + String.valueOf(N) + "个点中出现" + String.valueOf(K) + "个标记值为积液的点");
            alarmRecord.setJh(diagnosis.getJh());
            alarmRecordMapper.insert(alarmRecord);
        }











    }
}
